# -*- coding: utf-8 -*-
"""
__mktime__ = '2019/4/14'
__author__ = 'Just'
__filename__ = 'client'
客户端注册
"""
from app.libs.enums import ClientTypeEnum
from app.libs.error_code import Success, ParameterException
from app.libs.redprint import Redprint
from app.services.clientservice import ClientService
from app.validators.form import ClientForm, ClientEmailForm, ClientMinaForm


api = Redprint('client')


@api.route('/register', methods=['POST'])
def create_client():
    """注册入口"""
    form = ClientForm().validate_for_api()
    promise = {
        ClientTypeEnum.USER_EMAIL: __register_by_email,
        ClientTypeEnum.USER_MINA: __register_by_mina
    }
    _type = form.type.data
    if _type not in promise.keys():
        raise ParameterException(msg='抱歉,请检查请求注册的客户端类型!', error_code=1010)

    promise[form.type.data]()
    return Success(code=201, msg='注册成功')


def __register_by_email():
    """邮箱注册"""
    form = ClientEmailForm().validate_for_api()
    service = ClientService(form)
    service.register_by_email()


def __register_by_mina():
    """微信小程序注册"""
    form = ClientMinaForm().validate_for_api()
    service = ClientService(form)
    service.register_by_mina()
